function executeSql(obj) {
    var id = $(obj).attr("funId");
    var formId = 'executeFunctionForm' + id;
    var sql = $('#' + formId).find(":input")[2].value;
    $.ajax({
        type: "POST",
        url: "/function/executeSql",
        data: "sql=" + sql,
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
                return;
            }

            // $('#executeMsg' + id).empty();
            for(var key in rp.data){
                var content = '<div style="color:red">' + key + '</div>';
                var value = rp.data[key];
                if(!(value instanceof Array)){
                    content += '<div>' + rp.data[key] + '</div>';
                    $('#executeMsg' + id).append(content);
                    continue;
                }

                for (var item in value) {
                    content += value[item] + '<br/>';
                }
                $('#executeMsg' + id).append(content);
            }
        }
    });
}

/**
 * 删除框弹出时，将 funId 设置到隐藏域中
 * @param obj
 */
function preDeleteFunction(obj) {
    var funId = $(event.target).attr("funId"); // 触发事件的按钮
    $("#deleteFunDialog").find(":input[name='funId']").val(funId);
}

function executeDeleteFun(obj) {
    var id = $("#deleteFunDialog").find(":input[name='funId']").val();
    $.ajax({
        type: "POST",
        url: "/function/delete",
        data: "id=" + id,
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
                return;
            }
            alert("删除成功");
            window.location.reload();
        }

    });
}


/**
 * 执行保存功能实体
 */
function executeSaveFunction(entity) {
    $.ajax({
        type: "POST",
        url: "/function/save",
        data: JSON.stringify(entity),
        contentType: "application/json",
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
                return;
            }
            alert("保存成功");
            window.location.reload();
        }

    });
}

function updateFunction(obj){
    var id = $(obj).attr("funId");
    var name = $('#executeFunctionForm' + id).find(':input')[0].value;
    var desc = $('#executeFunctionForm' + id).find(':input')[1].value;
    var content = $('#executeFunctionForm' + id).find(':input')[2].value;

    var obj = new Object();
    obj.id = id;
    obj.name = name;
    obj.desc = desc;
    obj.content = content;

    executeSaveFunction(obj);
}

/**
 * 动态菜单切换时，样式控制
 */
function toggleMenuItem(){
    $(".meun-item").each(function () {
        $(this).removeClass("meun-item-active");
    });

    $(event.target).addClass("meun-item-active");
}


$(function ($) {

    // 动态添加功能菜单
    $.ajax({
        type: "GET",
        url: "/function/list",
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
                return;
            }
            var data = rp.data;
            for (var i = 0; i < data.length; i++) {
                var menuId = 'dynamic' + data[i].id;
                var menu = '<div onclick="toggleMenuItem()" class="meun-item" href="#' + menuId + '" aria-controls="' + menuId + '" role="tab" data-toggle="tab"><img src="images/icon_rule_grey.png">' + data[i].name + '</div>';
                $('#leftMeun').append(menu);

                // 添加菜单主体内容
                var menuBody = '<div role="tabpanel" class="tab-pane" id="' + menuId + '">\n' +
                    '                <div class="check-div">\n' +
                    '                    执行|修改功能：' + data[i].name +
                    '                </div>\n' +
                    '                <div style="padding: 50px 0;margin-top: 50px;background-color: #fff; text-align: right;width: 850px;margin: 50px auto;">\n' +
                    '                    <form id="executeFunctionForm' + data[i].id + '" class="form-horizontal">\n' +
                    '                        <div class="form-group">\n' +
                    '                            <label class="col-xs-4 control-label">功能名称：</label>\n' +
                    '                            <div class="col-xs-5">\n' +
                    '                                <input type="" class="form-control input-sm duiqi" value="' + data[i].name + '" style="margin-top: 7px;">\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                        <div class="form-group">\n' +
                    '                            <label class="col-xs-4 control-label">描述：</label>\n' +
                    '                            <div class="col-xs-6">\n' +
                    '                                <textarea class="form-control input-sm duiqi2">' + data[i].desc + '</textarea>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                        <div class="form-group">\n' +
                    '                            <label class="col-xs-4 control-label">内容：</label>\n' +
                    '                            <div class="col-xs-6 ">\n' +
                    '                                <textarea class="form-control input-sm duiqi2" rows="5">' + data[i].content + '</textarea>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                        <div class="form-group text-right">\n' +
                    '                            <div class="col-xs-offset-4 col-xs-5" style="margin-left: 169px;">' +
                    '                                <button type="button" funId="' + data[i].id + '" onclick="preDeleteFunction(this)" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#deleteFunDialog">删除</button>' +
                    '                                <button type="button" funId="' + data[i].id + '" onclick="updateFunction(this)" class="btn btn-xs btn-warning">保 存</button>\n' +
                    '                                <button type="button" funId="' + data[i].id + '" onclick="executeSql(this)" class="btn btn-xs btn-green">执 行</button>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                    </form>\n' +
                    '                </div>\n' +
                    '<div id="executeMsg' + data[i].id + '"></div>\n' +
                    '            </div>';

                $('#rightContent').find(".tab-content").append(menuBody);
            }

            // 删除功能警告窗口
            var modalBox =
                '<div class="modal fade" id="deleteFunDialog" role="dialog" aria-labelledby="gridSystemModalLabel">\n' +
                '    <div class="modal-dialog" role="document">\n' +
                '        <div class="modal-content">\n' +
                '            <div class="modal-header">\n' +
                '                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                '                <h4 class="modal-title">提示</h4>\n' +
                '            </div>\n' +
                '            <div class="modal-body">\n' +
                '                <div class="container-fluid">\n' +
                '                    确定要删除该功能？删除后不可恢复！\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="modal-footer">\n' +
                '                <button type="button" class="btn btn-xs btn-white" data-dismiss="modal">取 消</button>\n' +
                '                <button type="button" class="btn btn-xs btn-danger" onclick="executeDeleteFun(this)">确 定</button>\n' +
                '                <input type="hidden" name="funId">\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>\n' +
                '</div>';

            $('#rightContent').find(".tab-content").append(modalBox);
        }

    });


    // 给保存按钮绑定事件
    $('#addFunctionBtn').bind("click", function(){
        var name = $('#addFunctionForm').find(':input')[0].value;
        var desc = $('#addFunctionForm').find(':input')[1].value;
        var content = $('#addFunctionForm').find(':input')[2].value;

        var obj = new Object();
        obj.name = name;
        obj.desc = desc;
        obj.content = content;

        executeSaveFunction(obj);
    });


});